﻿var m_project_id = '';
var m_yxkzh = '';
var m_cksqdj = null;
$("document").ready(function () {
    loadData();
});

function loadData() {
    m_cksqdj = new SmObjectData("CKQDJK", "采矿申请登记");
    m_project_id = $.query.get("PROJECT_ID");

    var blhj = $.query.get("BLHJ");
    if (blhj != "SLXM") {
        SetFMReadonly();
        $(".m_btn").addClass("nosee");
    }
    else {
        $("input.dateinput").click(function () { WdatePicker({ dateFmt: 'yyyy-M-d' }); });
    }
    if (m_project_id != null && m_project_id != '') {
//        var dbalias = "CKQDJK";
//        var tablename = "采矿申请登记";
//        var condittion = "PROJECT_ID=\\\'" + m_project_id + "\\\'";
        //        GetFirstSmObject(dbalias, tablename, condittion, InitDJS);
        //    m_cksqdj.SetValue("SQR", $("input[name='txtSQR']").val() == null ? '' : $("input[name='txtSQR']").val());
        //    m_cksqdj.SetValue("KSMC", $("input[name='txtKSMC']").val() == null ? '' : $("input[name='txtKSMC']").val());
        //    m_cksqdj.SetValue("YXKZH", $("input[name='txtYXKZH']").val() == null ? '' : $("input[name='txtYXKZH']").val());
        //    m_cksqdj.SetValue("TBSJ", $("input[name='txtTBSJ']").val() == null ? '' : $("input[name='txtTBSJ']").val());
        //    m_cksqdj.SetValue("TBR", $("input[name='txtTBR']").val() == null ? '' : $("input[name='txtTBR']").val());
        m_cksqdj.GetFirstSmObject("PROJECT_ID=\\\'" + m_project_id + "\\\'");
        $("input[name='txt申请人']").val(m_cksqdj.GetValue("申请人"));
        $("input[name='txt矿山名称']").val(m_cksqdj.GetValue("矿山名称"));
        $("input[name='txt原许可证号']").val(m_cksqdj.GetValue("原许可证号"));
        $("input[name='txt填表时间']").val(m_cksqdj.GetValue("填表时间"));
        $("input[name='txt填表人']").val(m_cksqdj.GetValue("填表人"));
    }
    $("input.dateinput").val(new Date().toLocaleString());
}

function InitDJS(objstr) {
    var obj = JSON.parse(objstr);
    if (obj != null && obj.Data != null && obj.Data.length > 0) {
        var yxkzh = "";
        for (i = 0; i < obj.Data.length; i++) {
            var key = obj.Data[i].Key;
            if (key == "申请人" || key == "矿山名称" || key == "原许可证号" || key == "填表时间" || key == "填表人") {
                $("input[name='txt" + key + "']").val(obj.Data[i].Value);
                if (key == "原许可证号") {
                    yxkzh = obj.Data[i].Value;
                }
            }
        }
        if ($("input[name='txt填表时间']").val() == "") {
            $("input[name='txt填表时间']").val(new Date().getFullYear() + "-" + parseInt(new Date().getMonth() + 1) + "-" + new Date().getDate());
        }
    }
}

function InitDJSFromExistPrj(objstr) {
    var obj = JSON.parse(objstr);
    if (obj != null && obj.Data != null && obj.Data.length > 0) {
        for (i = 0; i < obj.Data.length; i++) {
            var key = obj.Data[i].Key;
            if (key == "申请人" || key == "矿山名称" || key == "许可证号" || key == "填表时间" || key == "填表人") {
                $("input[name='txt" + key + "']").val(obj.Data[i].Value);
                if (key == "许可证号") {
                    $("input[name='txtYXKZH']").val(obj.Data[i].Value);
                }
            }
        }
        if ($("input[name='txt填表时间']").val() == "") {
            $("input[name='txt填表时间']").val(new Date().getFullYear() + "-" + parseInt(new Date().getMonth() + 1) + "-" + new Date().getDate());
        }
    }
}
function SetFMReadonly() {
    $("input").attr("readonly", "readonly");
}
function selectDJXM() {
    var sel = new selDJXM();
    sel.Show(selDJXMCallBack);
}
function selDJXMCallBack(kqfwid) {
    //选择一个登记项目，把选择的登记项目的许可证号赋值给这个变更项目的原许可证号
    m_yxkzh = kqfwid;
    try {
        copy(kqfwid);
        var dbalias = "CKQDJK";
        var tablename = "采矿申请登记";
        var condittion = "BSM=\\\'" + kqfwid + "\\\'";
        //GetFirstSmObject(dbalias, tablename, condittion, InitDJSFromExistPrj);
        loadData();
    }
    catch (ee) {
    }
}

function copy(kqfwid) {
    $.ajax({
        url: DBCenterServiceUrl + "/CKQBG.asmx/CopyCKQBG",
        type: "POST",
        dataType: "json",
        data: "{'project_id':'" + m_project_id + "','KQFWID':'" + kqfwid + "'}",
        contentType: "application/json; charset=utf-8",
        success: function (json) {

        },
        error: function (x, e) {
        },
        complete: function (x) {
        }
    });
}

function saveBGXM() {
//    var strkeys = "SQR,KSMC,YXKZH,TBSJ,TBR";
//    var values = '';
//    values += $("input[name='txtSQR']").val() == null ? ' ,' : $("input[name='txtSQR']").val() + ",";
//    values += $("input[name='txtKSMC']").val() == null ? ' ,' : $("input[name='txtKSMC']").val() + ",";
//    values += $("input[name='txtYXKZH']").val() == null ? ' ,' : $("input[name='txtYXKZH']").val() + ",";
//    values += $("input[name='txtTBSJ']").val() == null ? ' ,' : $("input[name='txtTBSJ']").val() + ",";
//    values += $("input[name='txtTBR']").val() == null ? ' ,' : $("input[name='txtTBR']").val();
//    var url = '';
//    if (m_yxkzh == null || m_yxkzh == '') {
//        var params = "{'project_id':'" + m_project_id + "','keystrs':'" + strkeys + "','valuestrs':'" + values + "'}";
//        url = DBCenterServiceUrl + "/KZ_SPDJ/KZ_SPDJ_CKQBG.asmx/SaveProject";
//    } else {
//        var dbalias = "CKQDJK";
//        var tablename = "采矿申请登记";
//        var condittion = "KQFWID=\\\'" + m_yxkzh + "\\\'";
//        var v_YXKZH = "";
//        GetFirstSmObject(dbalias, tablename, condittion, function () {
//            var obj = JSON.parse(json.d);
//            if (obj != null && obj.Data != null && obj.Data.length > 0) {
//                var key = obj.Data[i].Key;
//                if (key == "XKZH") {
//                    v_YXKZH = obj.Data[i].Value;
//                }
//            }
//        });
//        var params = "{'project_id':'" + m_project_id + "','xkzh':'" + v_YXKZH + "','keystrs':'" + strkeys + "','valuestrs':'" + values + "'}";
//        url = DBCenterServiceUrl + "/KZ_SPDJ/KZ_SPDJ_CKQBG.asmx/CreateProjFromExist";
//        }
//        connectajax(url, function (obj) { alertex(obj); }, params);
    m_cksqdj.SetValue("申请人", $("input[name='txt申请人']").val() == null ? '' : $("input[name='txt申请人']").val());
    m_cksqdj.SetValue("矿山名称", $("input[name='txt矿山名称']").val() == null ? '' : $("input[name='txt矿山名称']").val());
    m_cksqdj.SetValue("原许可证号", $("input[name='txt原许可证号']").val() == null ? '' : $("input[name='txt原许可证号']").val());
    m_cksqdj.SetValue("填表时间", $("input[name='txt填表时间']").val() == null ? '' : $("input[name='txt填表时间']").val());
    m_cksqdj.SetValue("填表人", $("input[name='txt填表人']").val() == null ? '' : $("input[name='txt填表人']").val());
    var res = m_cksqdj.Update();
    alert(res);
}